package cn.sinata.wanglin.neighbor.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.easeui.domain.EaseEmojicon;
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class EditCommentActivity extends DialogActivity {

    @BindView(R.id.ease_layout_emojicon_menu)
    EaseEmojiconMenu emojiconMenu;
    @BindView(R.id.et_comment)
    EditText et_comment;
    @BindView(R.id.expressionView)
    TextView expressionView;
    @BindView(R.id.tv_action)
    TextView tv_action;
    private String userId;
    private String id;
    private String toUser;
    private int type ;
    private int position;
    private String name;
    private String commentId;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_edit_comment;
    }

    @Override
    protected void initView() {
        getWindow().setGravity(Gravity.BOTTOM);

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        id = getIntent().getStringExtra("id");
        toUser = getIntent().getStringExtra("toUser");
        type = getIntent().getIntExtra("type",0);
        position = getIntent().getIntExtra("position",0);
        name = getIntent().getStringExtra("name");
        commentId = getIntent().getStringExtra("commentId");

        List<EaseEmojiconGroupEntity> emojiconGroupList = new ArrayList<>();
        emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1,  Arrays.asList(EaseDefaultEmojiconDatas.getData())));
        emojiconMenu.init(emojiconGroupList);
        emojiconMenu.setEmojiconMenuListener(new EaseEmojiconMenuBase.EaseEmojiconMenuListener() {
            @Override
            public void onExpressionClicked(EaseEmojicon emojicon) {
//                et_comment.append(EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
                int selection = et_comment.getSelectionEnd();
                et_comment.getText().insert(selection,EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
            }

            @Override
            public void onDeleteImageClicked() {
                if (!TextUtils.isEmpty(et_comment.getText())) {
                    KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                    et_comment.dispatchKeyEvent(event);
                }
            }
        });
        if (type == 1) {
            if (!userId.equals(toUser)) {
                et_comment.setHint("回复"+name);
            }
        }
    }

    @OnTextChanged(R.id.et_comment)
    void onCommentChanged(Editable s){
        if (TextUtils.isEmpty(s)) {
            tv_action.setVisibility(View.GONE);
        } else {
            tv_action.setVisibility(View.VISIBLE);
        }
    }

    void addComment(){
        String content = et_comment.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            showToast("发送文字不能为空");
            return;
        }
        showDialog();
        HttpManager.addComment(userId,"C",id,toUser,content,type,"0",commentId)
                .subscribe(new ResultDataSubscriber<Comment>(this) {
                    @Override
                    public void onSuccess(String msg, Comment comment) {
                        showToast(msg);
                        Intent intent = new Intent();
                        intent.putExtra("position",position);
                        intent.putExtra("data",comment);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });
    }

    @OnClick({R.id.expressionView,R.id.et_comment,R.id.tv_action})
    void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.expressionView:
                Utils.hideSoftWindow(this,v);
                if (emojiconMenu.getVisibility() == View.VISIBLE) {
                    emojiconMenu.setVisibility(View.GONE);
                } else {
                    emojiconMenu.setVisibility(View.VISIBLE);
                    et_comment.requestFocus();
                }
                break;
            case R.id.et_comment:
                emojiconMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_action:
                addComment();
                break;
        }
    }
}
